﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service.Interface;
using Domain;
using Clannan.Models.ViewModel;
using Clannan.Models.JsonModel;

namespace Clannan.Controllers
{
    public class CommunityController : Controller
    {
        public IUserInfoService UserInfoService;
        public INoteBookService NotebookService;
        public IAreaService AreaService;
        public IPageService PageService;
        public IReplyService ReplyService;

        /// <summary>
        /// 社区首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            int pageCount;//总页数

            //获取分页数据
            IList<Page> pageList = this.PageService.LoadAllSharePage(1, 30, out pageCount);
            //获取用户个人信息
            UserInfo userInfo = this.UserInfoService.Get(User.Identity.Name);

            return View(new CommunityModel { PageList = pageList, UserInfo = userInfo });
        }


        /// <summary>
        /// 共享页详情
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <returns></returns>
        public ActionResult Detail(string pageID)
        {
            Page page = this.PageService.Get(pageID);
            //获取用户个人信息
            UserInfo userInfo = this.UserInfoService.Get(page.Area.NoteBook.UserInfo.ID);

            return View(new CommunityModel { Page = page, UserInfo = userInfo });
        }

        /// <summary>
        /// 获取回复分页集合
        /// </summary>
        /// <param name="pageID">共享页ID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        public JsonResult GetReply(string pageID,string pageIndex,string pageSize)
        {
            List<ReplyJsonModel> replyList = new List<ReplyJsonModel>();//Json返回页集合
            ReplyJsonModel replyJsonModel;//Json模型
            int replyCount;//回复总页数

            //获取回复列表
            IList<Reply> replyListData = this.ReplyService.LoadAllReply(Convert.ToInt32(pageID), Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize), out replyCount);
            //数据处理
            foreach (Reply reply in replyListData)
            {
                replyJsonModel = new ReplyJsonModel();
                replyJsonModel = replyJsonModel.Copy(replyJsonModel, reply);
                replyList.Add(replyJsonModel);
            }

            return Json(new { Message = 1, ReplyList = replyList, ReplyCount = replyCount });
        }

        /// <summary>
        /// 个人共享页
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns></returns>
        public ActionResult List(string userID)
        {
            int pageCount;//总页数

            //获取分页数据
            IList<Page> pageList = this.PageService.LoadAllSharePage(Convert.ToInt32(userID), 1, 30, out pageCount);
            //获取用户个人信息
            UserInfo userInfo = this.UserInfoService.Get(User.Identity.Name);

            return View(new CommunityModel { PageList = pageList, UserInfo = userInfo });
        }

        /// <summary>
        /// 获取共享页分页集合
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        public JsonResult GetAll(string userID, string pageIndex, string pageSize)
        {
            List<PageJsonModel> pageList = new List<PageJsonModel>();//Json返回页集合
            PageJsonModel pageJsonModel;                             //Json模型
            int pageCount;                                           //总页数
            IList<Page> pageListData;                                //获取的分页数据

            //获取个人页分页集合或是所有页分页集合
            if (userID == null)
            {
                pageListData = this.PageService.LoadAllSharePage(Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize), out pageCount);
            }
            else
            {
                pageListData = this.PageService.LoadAllSharePage(Convert.ToInt32(userID), Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize), out pageCount);
            }
            //数据处理
            foreach (Page page in pageListData)
            {
                pageJsonModel = new PageJsonModel();
                pageJsonModel = pageJsonModel.Copy(pageJsonModel, page);
                pageList.Add(pageJsonModel);
            }

            return Json(new { Message = 1, PageList = pageList, PageCount = pageCount });
        }

        /// <summary>
        /// 共享页回复
        /// </summary>
        /// <param name="pageID">共享页ID</param>
        /// <param name="content">回复内容</param>
        /// <returns></returns>
        public JsonResult Reply(string pageID, string content)
        {
            Reply reply = new Reply();
            reply.Content = content;
            reply.Time = DateTime.Now;
            reply.Page = new Page { ID = Convert.ToInt32(pageID) };
            reply.UserInfo = new UserInfo { ID = Convert.ToInt32(User.Identity.Name) };
            //保存回复同时更新共享页回复数
            this.ReplyService.SaveReplyAndUpdatePageReplyCount(reply);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 查询当前回复数，如果回复数增加则提醒有新回复
        /// </summary>
        /// <param name="pageID">共享页ID</param>
        /// <returns></returns>
        public JsonResult SelectReplyCount(string pageID)
        {
            int replyCount =  this.ReplyService.SelectReplyCount(Convert.ToInt32(pageID));

            return Json(new { Message = 1, ReplyCount = replyCount });
        }
    }
}
